<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>conditionaldelay.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_async_conditionaldelay.js.html">conditionaldelay.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Defines a class useful for handling functions that must be
<a name="line17"></a> * invoked later when some condition holds. Examples include deferred function
<a name="line18"></a> * calls that return a boolean flag whether it succedeed or not.
<a name="line19"></a> *
<a name="line20"></a> * Example:
<a name="line21"></a> *
<a name="line22"></a> *  function deferred() {
<a name="line23"></a> *     var succeeded = false;
<a name="line24"></a> *     // ... custom code
<a name="line25"></a> *     return succeeded;
<a name="line26"></a> *  }
<a name="line27"></a> *
<a name="line28"></a> *  var deferredCall = new goog.async.ConditionalDelay(deferred);
<a name="line29"></a> *  deferredCall.onSuccess = function() {
<a name="line30"></a> *    alert(&#39;Success: The deferred function has been successfully executed.&#39;);
<a name="line31"></a> *  }
<a name="line32"></a> *  deferredCall.onFailure = function() {
<a name="line33"></a> *    alert(&#39;Failure: Time limit exceeded.&#39;);
<a name="line34"></a> *  }
<a name="line35"></a> *
<a name="line36"></a> *  // Call the deferred() every 100 msec until it returns true,
<a name="line37"></a> *  // or 5 seconds pass.
<a name="line38"></a> *  deferredCall.start(100, 5000);
<a name="line39"></a> *
<a name="line40"></a> *  // Stop the deferred function call (does nothing if it&#39;s not active).
<a name="line41"></a> *  deferredCall.stop();
<a name="line42"></a> *
<a name="line43"></a> */
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>goog.provide(&#39;goog.async.ConditionalDelay&#39;);
<a name="line47"></a>
<a name="line48"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line49"></a>goog.require(&#39;goog.async.Delay&#39;);
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * A ConditionalDelay object invokes the associated function after a specified
<a name="line55"></a> * interval delay and checks its return value. If the function returns
<a name="line56"></a> * {@code true} the conditional delay is cancelled and {@see #onSuccess}
<a name="line57"></a> * is called. Otherwise this object keeps to invoke the deferred function until
<a name="line58"></a> * either it returns {@code true} or the timeout is exceeded. In the latter case
<a name="line59"></a> * the {@see #onFailure} method will be called.
<a name="line60"></a> *
<a name="line61"></a> * The interval duration and timeout can be specified each time the delay is
<a name="line62"></a> * started. Calling start on an active delay will reset the timer.
<a name="line63"></a> *
<a name="line64"></a> * @param {function():boolean} listener Function to call when the delay
<a name="line65"></a> *     completes. Should return a value that type-converts to {@code true} if
<a name="line66"></a> *     the call succeeded and this delay should be stopped.
<a name="line67"></a> * @param {Object=} opt_handler The object scope to invoke the function in.
<a name="line68"></a> * @constructor
<a name="line69"></a> * @extends {goog.Disposable}
<a name="line70"></a> */
<a name="line71"></a>goog.async.ConditionalDelay = function(listener, opt_handler) {
<a name="line72"></a>  goog.Disposable.call(this);
<a name="line73"></a>
<a name="line74"></a>  /**
<a name="line75"></a>   * The function that will be invoked after a delay.
<a name="line76"></a>   * @type {function():boolean}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.listener_ = listener;
<a name="line80"></a>
<a name="line81"></a>  /**
<a name="line82"></a>   * The object context to invoke the callback in.
<a name="line83"></a>   * @type {Object|undefined}
<a name="line84"></a>   * @private
<a name="line85"></a>   */
<a name="line86"></a>  this.handler_ = opt_handler;
<a name="line87"></a>
<a name="line88"></a>  /**
<a name="line89"></a>   * The underlying goog.async.Delay delegate object.
<a name="line90"></a>   * @type {goog.async.Delay}
<a name="line91"></a>   * @private
<a name="line92"></a>   */
<a name="line93"></a>  this.delay_ = new goog.async.Delay(
<a name="line94"></a>      goog.bind(this.onTick_, this), 0 /*interval*/, this /*scope*/);
<a name="line95"></a>};
<a name="line96"></a>goog.inherits(goog.async.ConditionalDelay, goog.Disposable);
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * The delay interval in milliseconds to between the calls to the callback.
<a name="line101"></a> * Note, that the callback may be invoked earlier than this interval if the
<a name="line102"></a> * timeout is exceeded.
<a name="line103"></a> * @type {number}
<a name="line104"></a> * @private
<a name="line105"></a> */
<a name="line106"></a>goog.async.ConditionalDelay.prototype.interval_ = 0;
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * The timeout timestamp until which the delay is to be executed.
<a name="line111"></a> * A negative value means no timeout.
<a name="line112"></a> * @type {number}
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.async.ConditionalDelay.prototype.runUntil_ = 0;
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * True if the listener has been executed, and it returned {@code true}.
<a name="line120"></a> * @type {boolean}
<a name="line121"></a> * @private
<a name="line122"></a> */
<a name="line123"></a>goog.async.ConditionalDelay.prototype.isDone_ = false;
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/** @override */
<a name="line127"></a>goog.async.ConditionalDelay.prototype.disposeInternal = function() {
<a name="line128"></a>  this.delay_.dispose();
<a name="line129"></a>  delete this.listener_;
<a name="line130"></a>  delete this.handler_;
<a name="line131"></a>  goog.async.ConditionalDelay.superClass_.disposeInternal.call(this);
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Starts the delay timer. The provided listener function will be called
<a name="line137"></a> * repeatedly after the specified interval until the function returns
<a name="line138"></a> * {@code true} or the timeout is exceeded. Calling start on an active timer
<a name="line139"></a> * will stop the timer first.
<a name="line140"></a> * @param {number=} opt_interval The time interval between the function
<a name="line141"></a> *     invocations (in milliseconds). Default is 0.
<a name="line142"></a> * @param {number=} opt_timeout The timeout interval (in milliseconds). Takes
<a name="line143"></a> *     precedence over the {@code opt_interval}, i.e. if the timeout is less
<a name="line144"></a> *     than the invocation interval, the function will be called when the
<a name="line145"></a> *     timeout is exceeded. A negative value means no timeout. Default is 0.
<a name="line146"></a> */
<a name="line147"></a>goog.async.ConditionalDelay.prototype.start = function(opt_interval,
<a name="line148"></a>                                                       opt_timeout) {
<a name="line149"></a>  this.stop();
<a name="line150"></a>  this.isDone_ = false;
<a name="line151"></a>
<a name="line152"></a>  var timeout = opt_timeout || 0;
<a name="line153"></a>  this.interval_ = Math.max(opt_interval || 0, 0);
<a name="line154"></a>  this.runUntil_ = timeout &lt; 0 ? -1 : (goog.now() + timeout);
<a name="line155"></a>  this.delay_.start(
<a name="line156"></a>      timeout &lt; 0 ? this.interval_ : Math.min(this.interval_, timeout));
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Stops the delay timer if it is active. No action is taken if the timer is not
<a name="line162"></a> * in use.
<a name="line163"></a> */
<a name="line164"></a>goog.async.ConditionalDelay.prototype.stop = function() {
<a name="line165"></a>  this.delay_.stop();
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * @return {boolean} True if the delay is currently active, false otherwise.
<a name="line171"></a> */
<a name="line172"></a>goog.async.ConditionalDelay.prototype.isActive = function() {
<a name="line173"></a>  return this.delay_.isActive();
<a name="line174"></a>};
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>/**
<a name="line178"></a> * @return {boolean} True if the listener has been executed and returned
<a name="line179"></a> *     {@code true} since the last call to {@see #start}.
<a name="line180"></a> */
<a name="line181"></a>goog.async.ConditionalDelay.prototype.isDone = function() {
<a name="line182"></a>  return this.isDone_;
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Called when the listener has been successfully executed and returned
<a name="line188"></a> * {@code true}. The {@see #isDone} method should return {@code true} by now.
<a name="line189"></a> * Designed for inheritance, should be overridden by subclasses or on the
<a name="line190"></a> * instances if they care.
<a name="line191"></a> */
<a name="line192"></a>goog.async.ConditionalDelay.prototype.onSuccess = function() {
<a name="line193"></a>  // Do nothing by default.
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Called when this delayed call is cancelled because the timeout has been
<a name="line199"></a> * exceeded, and the listener has never returned {@code true}.
<a name="line200"></a> * Designed for inheritance, should be overridden by subclasses or on the
<a name="line201"></a> * instances if they care.
<a name="line202"></a> */
<a name="line203"></a>goog.async.ConditionalDelay.prototype.onFailure = function() {
<a name="line204"></a>  // Do nothing by default.
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * A callback function for the underlying {@code goog.async.Delay} object. When
<a name="line210"></a> * executed the listener function is called, and if it returns {@code true}
<a name="line211"></a> * the delay is stopped and the {@see #onSuccess} method is invoked.
<a name="line212"></a> * If the timeout is exceeded the delay is stopped and the
<a name="line213"></a> * {@see #onFailure} method is called.
<a name="line214"></a> * @private
<a name="line215"></a> */
<a name="line216"></a>goog.async.ConditionalDelay.prototype.onTick_ = function() {
<a name="line217"></a>  var successful = this.listener_.call(this.handler_);
<a name="line218"></a>  if (successful) {
<a name="line219"></a>    this.isDone_ = true;
<a name="line220"></a>    this.onSuccess();
<a name="line221"></a>  } else {
<a name="line222"></a>    // Try to reschedule the task.
<a name="line223"></a>    if (this.runUntil_ &lt; 0) {
<a name="line224"></a>      // No timeout.
<a name="line225"></a>      this.delay_.start(this.interval_);
<a name="line226"></a>    } else {
<a name="line227"></a>      var timeLeft = this.runUntil_ - goog.now();
<a name="line228"></a>      if (timeLeft &lt;= 0) {
<a name="line229"></a>        this.onFailure();
<a name="line230"></a>      } else {
<a name="line231"></a>        this.delay_.start(Math.min(this.interval_, timeLeft));
<a name="line232"></a>      }
<a name="line233"></a>    }
<a name="line234"></a>  }
<a name="line235"></a>};
</pre>


</body>
</html>
